<?php

class templates_model extends MY_Model {

    protected $table = 'templates'; //模板表
    protected $table1 = 'template_details'; //模板详细表

    function __construct() {
        parent::__construct();
    }

    /**
     * 列表过滤
     */
    public function list_filter($config = array()) 
    {
        /* 配置默认值设置 */
        $default_config = array(
            'select' => '*',
            'sort' => 'new',
            'get_all' => 0,
            'user_id' => '',
        );
        /* /配置默认值设置 */
        $config = array_merge($default_config, $config);

        $this->db->select($config['select']);
        return $config;
    }

    /**
     * 获取列表
     * $cur_page 当前页数
     * $per_page 每页显示
     */
    public function get_template_list($config = array(), $cur_page = 1, $per_page = 40) 
    {
        $config = $this->list_filter($config);
        switch ($config['sort']) { //排序
            case 'new':
                $this->db->order_by('id', "asc");
                break;
        }
        if ($config['get_all'] != 1) {
            $this->db->limit($per_page, ($cur_page - 1) * $per_page); //分页
        }
        $result = $this->db->get($this->table)->result_array();

        return $result;
    }
    
    /**
     * 获取单个模板所有模块
     */
    public function get_template_by_id($t_id)
    {
    	$this->db->where('t_id', $t_id);
    	$result = $this->db->get($this->table1)->result_array();
    	return $result;
    }
    
    /**
     * 获取模板详情
     */
    public function get_template_by_id_type($t_id, $type=0)
    {
    	$this->db->where('t_id', $t_id);
    	$this->db->where('type', $type);
    	$row = $this->db->get($this->table1)->row_array();
    	return $row;
    }
   
}
